<?php

namespace App\Admin\Controllers;

use App\Models\Activity;
use App\Models\Product;
use App\Models\BlindBoxCategory;
use App\Models\BlindBoxLevel;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Box;
use Illuminate\Support\Str;
use App\Admin\Actions\BlindBoxAction\RefreshBlindBoxRate;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Support\Facades\Storage;
use Encore\Admin\Controllers\AdminController;

class ActivitiesController extends AdminController
{
    protected $title = '活动信息';
    

    protected function grid()
    {
        $grid = new Grid(new Activity());
	
	    $grid->disableExport();
	    
	    $grid->column('name', '活动名称');
	    $grid->column('start_at', '活动开始时间');
	    $grid->column('end_at', '活动结束时间');
	    $grid->column('cover', '封面')->display(function ($value) {
	        if (! Storage::exists($value)) {
		       // $this->update(['cover' => null]);
		        return '';
	        } else {
		        return _grid_image($value);
	        }
        });
	    $grid->column('created_at', '创建时间')->display(function () {
	    	return $this->created_at->toDatetimeString();
	    });
	   	$this->gridActions($grid);
        return $grid;
    }
    

   	protected function gridActions(Grid $grid)
	{
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
			$actions->disableDelete();
			//$actions->add(new RefreshBlindBoxRate());
		});
	}

    protected function detail($id)
    {
        $show = new Show(Activity::findOrFail($id));
        return $show;
    }
    
    protected function form()
    {
        $form = new Form(new Activity());

	   //	$form->select('blind_box_category_id','盲盒分类')->options(BlindBoxCategory::all()->pluck('name', 'id'));

	    $form->text('name', '活动名称');
	 	$form->datetimeRange('start_at', 'end_at', '活动时间');
 		$form->textarea('data', '活动介绍')->rules('required|string');
 		$form->image('cover', '封面图片')
		    ->uniqueName()
		    ->creationRules('required');

        return $form;
    }

   
}
